import java.net.URL;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


public class MainAstraweb {
	public static final String STARTPAGE = "http://jokes.astraweb.com/display.cgi?r=209156623535&c=adult.1";
	public static final String DOMAIN = "http://jokes.astraweb.com";
	public static String toURL(String url){
		if(url.toLowerCase().startsWith("http://")){
			return url;
		}else{
			return DOMAIN + url	;
		}
	}
	public static void main(String[] args) {
		Document doc = Jsoup.parse(new URL(STARTPAGE), 1000 * 30);
		while (true) {
			Element nextPageLink = doc.select("a[title=前一篇]").first();
			Elements imageElements = doc.select("div#content img");
			Element titleElement = doc.select("div.title h2").first();
			String title = parseTitle(titleElement);
			String nextPageUrl = toURL( nextPageLink.attr("href"));
			System.out.println("page:" + nextPageUrl);
			downloadImages(imageElements, title);
			System.out.println("=================");
			URL nextPageURL = new URL(nextPageUrl);
			doc = Jsoup.parse(nextPageURL, 1000 * 30);
		}
	}

}
